/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import {isRunning} from './helper.js'
import {Task} from './Task.js'

"use strict";

var __importDefault = this && this.__importDefault || function (mod) {
    return mod && mod.__esModule ? mod : { "default": mod };
};

// const helper_1 = helper;
const Task_1 = __importDefault(Task);
let registry = {};
export function getRegistry() {
    return registry;
}
// exports.getRegistry = getRegistry;
export function registryLookUp(node) {
    if (typeof node === 'string') {
        const lookedUpNode = registry[node];
        if (!lookedUpNode) {
            throw new Error(`No node with name ${node} registered.`);
        }
        return lookedUpNode;
    }
    return node;
}
registryLookUp = registryLookUp;
class BehaviorTree {
    constructor({ tree, blackboard }) {
        this.tree = tree;
        this.blackboard = blackboard;
        this.lastResult = undefined;
    }
    step({ introspector } = {}) {
        const lastRun = this.lastResult && typeof this.lastResult === 'object' ? this.lastResult : undefined;
        const rerun = (0, isRunning)(this.lastResult);
        if (introspector) {
            introspector.start(this);
        }
        this.lastResult = registryLookUp(this.tree).run(this.blackboard, {
            lastRun,
            introspector,
            rerun,
            registryLookUp
        });
        if (introspector) {
            introspector.end();
        }
    }
    static register(name, node) {
        registry[name] = typeof node === 'function' ? new Task_1.default({ name, run: node }) : node;
    }
    static cleanRegistry() {
        registry = {};
    }
}
// exports.default = BehaviorTree;
export { BehaviorTree }
